# -*- coding: utf-8 -*-
'''
模块：获取酒店id，酒店名称
输入：cookie
输出：hotel_name,hotel_id
'''
import requests
import re
from fcs import url_request
import json
import re

def main(cookies):
    # # 获取酒店名
    # url = 'http://ebooking.ctrip.com/ebooking/Entry/index2.aspx'
    # # con = requests.get(url=url, cookies=cookies).text
    # while True:
    #     con = url_request.get_url_requests(url=url,cookies=cookies)
    #     print(con)
    #     c = re.compile('<span id="lblHotelName" class="ui-name">(.*?)</span>', re.S)
    #     name = re.findall(c, con)
    #     if name:
    #         name = name[0]
    #
    #         c = re.compile('href="http://hotels.ctrip.com/hotel/(\d*).', re.S)
    #         ids = re.findall(c, con)
    #         if ids:
    #             ids = ids[0]
    #         return(name, ids)
    #     else:
    #         continue

    # hotel_id 获取
    flag=0
    while flag<=5:
        try:
            con=url_request.get_url_requests(url="http://ebooking.ctrip.com/ebkassembly/ajax/CommonHeaderAndFooter.ashx",cookies=cookies)
            c=re.compile("<a id='aHotelName' target='_blank' href='//hotels.ctrip.com/hotel/(\d*).html#ctm_ref=chp_htl_3_n'>(.*?)</a>",re.S)
            datas=re.findall(c,con)
            name=datas[0][1]
            hotel_id=datas[0][0]
            return name,hotel_id
        except:
            flag+=1
            print('name error')
    else:
        return '未知异常', '未知异常'


    # flag=0
    # while True:
    #     try:
    #         res = url_request.post_url_requests(url='http://ebooking.ctrip.com/ebooking/Ajax/SideBar.ashx',data={'Method': 'GetBaseHotelInfo'}, headers={}, cookies=cookies)
    #         print(res)
    #         input('===')
    #         data = json.loads(res)
    #
    #         hotel_id = data['Data']['MasterHotelId']
    #
    #         while True:
    #             pc_url = 'http://hotels.ctrip.com/hotel/%s.html' % (str(hotel_id))
    #             print(pc_url)
    #             con = url_request.get_url_requests(url=pc_url, cookies=cookies)
    #             c = re.compile('itemprop="name">(.*?)</h2>', re.S)
    #             name = re.findall(c, con)
    #             if flag>5:
    #                 name=''
    #                 return name, hotel_id
    #             if name:
    #                 name = name[0]
    #                 name = str(name).replace('<span class="label_selection">精选</span>','')
    #                 return name, hotel_id
    #             else:
    #                 flag += 1
    #                 print('店名获取失败')
    #                 continue
    #     except:
    #         continue

